<?php

namespace app\store\model;

use app\common\model\Goods as GoodsModel;
use app\common\model\Ticket as ModelTicket;
use app\store\service\Goods as GoodsService;
use think\Cache;

/**
 * 门票模型
 * Class Ticket
 * @package app\store\model
 */
class Ticket extends ModelTicket
{
    protected $append = [];

    public static function init()
    {
        /**
         * 商品增加时自增
         */
        self::event('after_insert', function ($orders) {
            Cache::store('redis')->inc('goods_total');
        });
        /**
         * 商品软删除时自减
         */
        self::afterUpdate(function ($orders) {
            if( Cache::store('redis')->get('goods_total') == 0 ){
                Cache::store('redis')->inc('goods_total',0);
            }else{
                Cache::store('redis')->dec('goods_total');
            }
        });
    }

    /**
     * 添加门票
     * @param array $data
     * @return bool
     * @throws \think\exception\PDOException
     */
    public function add(array $data)
    {
   
        $data['wxapp_id'] = $data['sku']['wxapp_id'] = self::$wxapp_id;

        // 开启事务
        $this->startTrans();
        try {
            // 添加门票
            $this->allowField(true)->save($data);
            // // 商品规格
            // $this->addGoodsSpec($data);
            // // 商品图片
            // $this->addGoodsImages($data['images']);
            $this->commit();
            return true;
        } catch (\Exception $e) {
            $this->error = $e->getMessage();
            $this->rollback();
            return false;
        }
    }

    /**
     * 编辑门票
     * @param $data
     * @return bool|mixed
     */
    public function edit($data,$id)
    {
        // $data['spec_type'] = isset($data['spec_type']) ? $data['spec_type'] : $this['spec_type'];
        // $data['content'] = isset($data['content']) ? $data['content'] : '';
        // $data['wxapp_id'] = $data['sku']['wxapp_id'] = self::$wxapp_id;
        // return $this->transaction(function () use ($data) {
        //     // 保存门票
        //     $this->allowField(true)->save($data);
            
        //     return true;
        // });
        $data['wxapp_id'] = self::$wxapp_id;
        $res = $this->allowField(true)->save($data,['id'=>$id]);
        return $res;
    }

    /**
     * 修改门票状态
     * @param $state
     * @return false|int
     */
    public function setStatus($state)
    {
        return $this->allowField(true)->save(['status' => $state ? 10 : 20]) !== false;
    }

    /**
     * 软删除
     * @return false|int
     */
    public function setDelete()
    {
        return $this->allowField(true)->save(['is_delete' => 1]);
    }

    /**
     * 获取当前门票总数
     * @param array $where
     * @return int|string
     * @throws \think\Exception
     */
    public function getGoodsTotal($where = [])
    {
        return $this->where('is_delete', '=', 0)->where($where)->count();
    }

    /**
     * 获取列表数据
     * @param null $status
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function getList($status = null)
    {
        !is_null($status) && $this->where('status', '=', (int)$status);
        return $this->where('is_delete', '=', '0')
            ->order(['sort' => 'asc', 'create_time' => 'desc'])
            ->paginate(15, false, [
                'query' => \request()->request()
            ]);
    }
}
